package com.hncu.edu.info.manager.dao;

import com.hncu.edu.info.manager.domain.Teacher;

public class TeacherDao implements BaseTeacherDao {
    //加static是为了保证此类的任何对象都在使用这个数组
    static Teacher[] arr = new Teacher[5];

    static {
        Teacher teacher1 = new Teacher("hncu001", "张三", "40", "1980-10-10");
        Teacher teacher2 = new Teacher("hncu002", "李四", "50", "1970-11-11");
        arr[0] = teacher1;
        arr[1] = teacher2;
    }

    //空参构造
    public TeacherDao() {

    }

    //传递数组，查看老师
    public Teacher[] findAllTeacher() {
        return arr;
    }

    //添加学生
    public boolean addTeacher(Teacher teacher) {
        //假设数组已经存满
        int Index = -1;
        //遍历数组，找空位，并且记录空位的位置
        for (int i = 0; i < arr.length; i++) {
            Teacher t = arr[i];
            if (t == null) {
                Index = i;
                break;
            }
        }
        //数组如果满了，就传false
        if (Index == -1) {
            return false;
        } else {
            //将传过来的对象添加到空位中去
            arr[Index] = teacher;
            return true;
        }
    }

    //删除学生
    public void deleteTeacher(String deleteId) {
        //根据传来的学号找到相应的索引位置
        int Index = getIndex(deleteId);
        //将其删除
        arr[Index] = null;
    }

    //根据工号找索引位置
    public int getIndex(String Id) {
        //先给索引赋初值，表示不在数组中
        int Index = -1;
        //遍历数组，找到工号所对应的索引位置
        for (int i = 0; i < arr.length; i++) {
            Teacher teacher = arr[i];
            if (teacher != null && teacher.getid().equals(Id)) {
                Index = i;
            }
        }
        return Index;
    }

    //修改老师
    public void updateTeacher(Teacher teacher) {
        //将要修改的Id
        String Id = teacher.getid();
        //根据Id找到位置
        int Index = getIndex(Id);
        //将新的对象填入数组中
        arr[Index] = teacher;
    }
}
